home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / c / amigacc68k.lha / cc68k / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-02  |  5.3 KB  |  186 lines

  1. #include        "stdio.h"
  2. #include        "string.h"
  3. #include        "c.h"
  4. #include        "expr.h"
  5. #include        "gen.h"
  6. #include        "cglbdec.h"
  7.  
  8. /*
  9.  *    68000 C compiler
  10.  *
  11.  *    Copyright 1984, 1985, 1986 Matthew Brandt.
  12.  *  all commercial rights reserved.
  13.  *
  14.  *    This compiler is intended as an instructive tool for personal use. Any
  15.  *    use for profit without the written consent of the author is prohibited.
  16.  *
  17.  *    This compiler may be distributed freely for non-commercial use as long
  18.  *    as this notice stays intact. Please forward any enhancements or question
  19. s
  20.  *    to:
  21.  *
  22.  *        Matthew Brandt
  23.  *        Box 920337
  24.  *        Norcross, Ga 30092
  25.  */
  26.  
  27.  doinit(sp)
  28. SYM     *sp;
  29. {       dseg();                 /* initialize into data segment */
  30.         nl();                   /* start a new line in object */
  31.         if(sp->storage_class == sc_static)
  32.                 put_label((int)sp->value.i);
  33.         else
  34.                 gen_strlab(sp->name);
  35.         if( lastst != assign)
  36.                 genstorage((int)sp->tp->size);
  37.         else    {
  38.                 getsym();
  39.                 inittype(sp->tp);
  40.                 }
  41.         endinit();
  42. }
  43.  
  44. int     inittype(tp)
  45. TYP     *tp;
  46. {       int     nbytes;
  47.         switch(tp->type) {
  48.  
  49.                 case bt_char:
  50.                         nbytes = initchar();
  51.                         break;
  52.                 case bt_short:
  53.                 case bt_enum:
  54.                         nbytes = initshort();
  55.                         break;
  56.                 case bt_pointer:
  57.                         if( tp->val_flag)
  58.                                 nbytes = initarray(tp);
  59.                         else
  60.                                 nbytes = initpointer();
  61.                         break;
  62.                 case bt_long:
  63.                         nbytes = initlong();
  64.                         break;
  65.                 case bt_struct:
  66.                         nbytes = initstruct(tp);
  67.                         break;
  68.                 default:
  69.                         error(ERR_NOINIT);
  70.                         nbytes = 0;
  71.                 }
  72.         return nbytes;
  73. }
  74.  
  75. int initarray(tp)
  76. TYP     *tp;
  77. {       int     nbytes;
  78.         char    *p;
  79.         nbytes = 0;
  80.         if( lastst == begin) {
  81.                 getsym();               /* skip past the brace */
  82.                 while(lastst != end) {
  83.                         nbytes += inittype(tp->btp);
  84.                         if( lastst == comma)
  85.                                 getsym();
  86.                         else if( lastst != end)
  87.                                 error(ERR_PUNCT);
  88.                         }
  89.                 getsym();               /* skip closing brace */
  90.                 }
  91.         else if( lastst == sconst && tp->btp->type == bt_char) {
  92.                 nbytes = strlen(laststr) + 1;
  93.                 p = laststr;
  94.                 while( *p )
  95.                         genbyte(*p++);
  96.                 genbyte(0);
  97.                 getsym();
  98.                 }
  99.         else if( lastst != semicolon)
  100.                 error(ERR_ILLINIT);
  101.         if( nbytes < tp->size) {
  102.                 genstorage((int) (tp->size - nbytes));
  103.                 nbytes = (int)tp->size;
  104.                 }
  105.         else if( tp->size != 0 && nbytes > tp->size)
  106.                 error(ERR_INITSIZE);    /* too many initializers */
  107.         return nbytes;
  108. }
  109.  
  110. int initstruct(tp)
  111. TYP     *tp;
  112. {       SYM     *sp;
  113.         int     nbytes;
  114.         needpunc(begin);
  115.         nbytes = 0;
  116.         sp = tp->lst.head;      /* start at top of symbol table */
  117.         while(sp != 0) {
  118.                 while(nbytes < sp->value.i)     /* align properly */
  119.                         nbytes += genbyte(0);
  120.                 nbytes += inittype(sp->tp);
  121.                 if( lastst == comma)
  122.                         getsym();
  123.                 else if(lastst == end)
  124.                         break;
  125.                 else
  126.                         error(ERR_PUNCT);
  127.                 sp = sp->next;
  128.                 }
  129.         if( nbytes < tp->size)
  130.                 genstorage((int)( tp->size - nbytes));
  131.         needpunc(end);
  132.         return tp->size;
  133. }
  134.  
  135. int initchar()
  136. {       genbyte(intexpr());
  137.         return 1;
  138. }
  139.  
  140. int initshort()
  141. {       genword(intexpr());
  142.         return 2;
  143. }
  144.  
  145. int initlong()
  146. {       genlong(intexpr());
  147.         return 4;
  148. }
  149.  
  150. int initpointer()
  151. {       SYM     *sp;
  152.         if(lastst == and) {     /* address of a variable */
  153.                 getsym();
  154.                 if( lastst != id)
  155.                         error(ERR_IDEXPECT);
  156.                 else if( (sp =(SYM *) gsearch(lastid)) == 0)
  157.                         error(ERR_UNDEFINED);
  158.                 else    {
  159.                         getsym();
  160.                         if( lastst == plus || lastst == minus)
  161.                                 genref(sp,intexpr());
  162.                         else
  163.                                 genref(sp,0);
  164.                         if( sp->storage_class == sc_auto)
  165.                                 error(ERR_NOINIT);
  166.                         }
  167.                 }
  168.         else if(lastst == sconst) {
  169.                 gen_labref(stringlit(laststr));
  170.                 getsym();
  171.                 }
  172.         else
  173.                 genlong(intexpr());
  174.         endinit();
  175.         return 4;       /* pointers are 4 bytes long */
  176. }
  177.  
  178.  endinit()
  179. {       if( lastst != comma && lastst != semicolon && lastst != end) {
  180.                 error(ERR_PUNCT);
  181.                 while( lastst != comma && lastst != semicolon && lastst != end)
  182.                         getsym();
  183.                 }
  184. }
  185.  
  186.